package com.yonyou.pmclouds.statistic.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.DateUtils;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.project.rmiitf.ProjectCountRemoteService;
import com.yonyou.pmclouds.statistic.entity.DataReportVO;
import com.yonyou.pmclouds.statistic.entity.ProStateCountResultVO;
import com.yonyou.pmclouds.statistic.entity.ProblemReasonViewVO;
import com.yonyou.pmclouds.statistic.rmiitf.DataAnalyseRemoteService;
import com.yonyou.pmclouds.statistic.rmiitf.ProblemReasonAnalyseRemoteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value="/mobile/statistic")
public class StatisticController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectCountRemoteService projectCountRemoteService;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private DataAnalyseRemoteService dataAnalyseRemoteService;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProblemReasonAnalyseRemoteService problemReasonAnalyseRemoteService;
    @Autowired
    private UserInfoQuery userInfoQuery;

    private static final String[] USER_ID_FIELDS = {ProblemReasonViewVO.MANAGER};
    private static final String[] USER_NAME_FIELDS = {ProblemReasonViewVO.MANAGER_NAME};

    @ResponseBody
    @RequestMapping(value = "/proStateCount")
    public ProStateCountResultVO proStateStatistics(@RequestParam(value = "pkTenant") String pkTenant,
                                                    @RequestParam(value = "pkOrg") String pkOrg,
                                                    @RequestParam(value = "startDate",required = false) String startDate,
                                                    @RequestParam(value = "endDate",required = false) String endDate) throws BusinessException {

        if (StringUtils.isEmpty(pkTenant) && StringUtils.isEmpty(pkOrg))
            throw new BusinessException("未获取到当前租户和组织");

        if (StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
            String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
            startDate = year + "-01-01 00:00:00";

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            endDate = sdf.format(DateUtils.asDateEnd(new Date()));
        }
        ProStateCountResultVO proStateCountResultVO = projectCountRemoteService.getProStateCountVOs(pkTenant, pkOrg, startDate, endDate);
        return proStateCountResultVO;
    }

    @RequestMapping("/alerVisaProblemAnalyse")
    @ResponseBody
    public DataReportVO dataAnalyse(@RequestParam(name = "pkTenant") String pkTenant, @RequestParam(name = "pkOrg") String pkOrg,
                                    @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate) throws BusinessException{
        if (StringUtils.isEmpty(pkTenant) || StringUtils.isEmpty(pkOrg))
            throw new BusinessException("未获取到当前租户和组织");

        //对前台传过来的日期进行处理
        startDate = startDate + " 00:00:00";
        endDate = endDate + " 23:59:59";

        return dataAnalyseRemoteService.queryDataAnalyse(pkTenant, pkOrg, startDate, endDate);
    }

    @RequestMapping("/queryProblem")
    @ResponseBody
    public List<ProblemReasonViewVO> queryProblem(@RequestParam(name = "pkTenant") String pkTenant, @RequestParam(name = "pkOrg") String pkOrg, @RequestParam(name = "pkProblemReason") String[] pkProblemReason,
                                                  @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate) throws BusinessException{
        if (StringUtils.isEmpty(pkTenant) || StringUtils.isEmpty(pkOrg))
            throw new BusinessException("未获取到当前租户和组织");

        //对前台传过来的日期进行处理
        startDate = startDate + " 00:00:00";
        endDate = endDate + " 23:59:59";

        List<ProblemReasonViewVO> problemReasonViewList = problemReasonAnalyseRemoteService.queryProblemLogAnalyseByReason(pkTenant, pkOrg, pkProblemReason, startDate, endDate);
        userInfoQuery.tranUserInfoField(problemReasonViewList.toArray(new ProblemReasonViewVO[0]), USER_ID_FIELDS, USER_NAME_FIELDS);
        return problemReasonViewList;
    }
}


